//
//  WBNewFeatureViewController.swift
//  SinaWeibo_Swift
//
//  Created by TianZhen on 16/4/12.
//  Copyright © 2016年 TianZhen. All rights reserved.
//

import UIKit

class WBNewFeatureViewController: UIViewController {

    /** 屏幕宽度 */
    let screenW = UIScreen.mainScreen().bounds.width
    /** 屏幕高度 */
    let screenH = UIScreen.mainScreen().bounds.height
    
    static var result: NSMutableArray?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.setupUI()
    }
    
    private func setupUI()
    {
        // 添加到view
        view.addSubview(newFeatureScrollView)
    }
    
    /** 开始体验按钮 */
    lazy var startButton:UIButton = {
        // 创建按钮
        let startButton = UIButton(type: UIButtonType.Custom)
        // 创建图片
        let bacGroundImage = UIImage(named: "tabbar_compose_button")
        let bacGroundImageSel = UIImage(named: "new_feature_finish_button_highlighted")
        // 设置按钮图片
        startButton.setBackgroundImage(bacGroundImage, forState: UIControlState.Normal)
        startButton.setBackgroundImage(bacGroundImageSel, forState: UIControlState.Selected)
        // 设置文字
        startButton.setTitle("开始体验", forState: UIControlState.Normal)
        // frame
        let btnW = 128
        let btnX = (Int(self.screenW) - btnW) / 2
        let btnY = CGFloat(self.screenH) * 0.8
        let btnH = 69
        // 设置frame
        startButton.frame = CGRect(x: Int(btnX), y: Int(btnY), width: Int(btnW), height: Int(btnH))
        // 隐藏按钮
        startButton.hidden = true
        // 注册点击事件
        startButton.addTarget(self, action: "startClick", forControlEvents: UIControlEvents.TouchDown)
        return startButton
    }()
    
    // 开始体验 按钮点击事件
    func startClick() {
        (UIApplication.sharedApplication().delegate as? AppDelegate)?.switchViewController(true)
    }
    
    /** 滚动动画 */
    func showStartButton() {
        // 使用transform来动画
        startButton.transform = CGAffineTransformMakeScale(0, 0)
        // 动画
        UIView.animateWithDuration(1.2, delay: 0.05, usingSpringWithDamping: 0.6, initialSpringVelocity: 1, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
            self.startButton.transform = CGAffineTransformIdentity
            }, completion: { (_) -> Void in
                print("开始体验 动画结束")
        })
    }
    
    /** 滚动图片视图 */
    lazy var newFeatureScrollView: UIScrollView = {

        // 显示图片的滚动view
        let scrollView:UIScrollView = UIScrollView(frame: UIScreen.mainScreen().bounds)
        scrollView.delegate = self
        
        // 创建imageView
        let img1:UIImageView = UIImageView(image: UIImage(named: "new_feature_1"))
        let img2:UIImageView = UIImageView(image: UIImage(named: "new_feature_2"))
        let img3:UIImageView = UIImageView(image: UIImage(named: "new_feature_3"))
        let img4:UIImageView = UIImageView(image: UIImage(named: "new_feature_4"))
        
        // 添加到滚动view
        scrollView.addSubview(img1)
        scrollView.addSubview(img2)
        scrollView.addSubview(img3)
        scrollView.addSubview(img4)
        // 关闭滚动条
        scrollView.showsHorizontalScrollIndicator = false
        scrollView.showsVerticalScrollIndicator = false
        // 其他设置
        scrollView.bounces = false
        scrollView.pagingEnabled = true
        
        var index:CGFloat = 0.0
        // 布局
        for imageViewm: UIView in scrollView.subviews
        {   // 拿到imageView
            let xxx:UIImageView = imageViewm as! UIImageView
            // 赋值frame
            xxx.frame = CGRect(x: index * self.screenW, y: 0, width: self.screenW, height: self.screenH)
            index++
        }
        // 添加按钮
        img4.addSubview(self.startButton)
        img4.userInteractionEnabled = true
        // 视图滚动范围
        scrollView.contentSize = CGSize(width: 4 * self.screenW, height: 0)
        
        return scrollView
    }()
}

var token: dispatch_once_t = 0
extension WBNewFeatureViewController: UIScrollViewDelegate
{
    func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
        
        // 获取当前显示的页码
        let index = Int(scrollView.contentOffset.x / scrollView.bounds.width)
        // 判断是否是最后一页
        if index == self.newFeatureScrollView.subviews.count - 1 {
            startButton.hidden = false
            
            dispatch_once(&token, { () -> Void in
                self.showStartButton()
            })
        }
    }
}